package com.zzwtel.autocode.beetl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.beetl.core.Configuration;
import org.beetl.core.GroupTemplate;
import org.beetl.core.Template;
import org.beetl.core.resource.FileResourceLoader;

import com.zzwtel.autocode.template.constants.TemplatePath;
import com.zzwtel.autocode.template.model.EntityModel;
import com.zzwtel.autocode.template.model.Property;
import com.zzwtel.autocode.util.FileUtil;
import com.zzwtel.autocode.util.HumpUtil;
import com.zzwtel.autocode.util.PathUtil;
import com.zzwtel.autocode.util.StrKit;

/**
 * 生成实体的模版
 * @author 草原狼
 * @date 2017-6-26
 */
public class EntityTemplate {

	public void generate(EntityModel tm) {
		try{
			//准备工作#######################
			String root = PathUtil.getTemplateRoot();
			FileResourceLoader resourceLoader = new FileResourceLoader(root,"utf-8");
			Configuration cfg = Configuration.defaultConfiguration();
			GroupTemplate gt = new GroupTemplate(resourceLoader, cfg);
			//Template t = gt.getTemplate("/html/@{entity_model}_edit_form.html");	
			Template t = gt.getTemplate(TemplatePath.ENTITY_JAVA_FILE_TEMPLATE);			
			String modelPkg = "com.zzwtec.information.model";
			//绑定数据#######################
			t.binding("tableRemark", tm.getTable().getRemark());
			t.binding("modelPkg", modelPkg);			
			String model = tm.getTable().getModel();			
			//首字母小写，驼峰命名
			t.binding("entityModel", model);
			//首字母大写，驼峰命名
			t.binding("EntityModel", StrKit.firstCharToUpperCase(model));
			//全部大写，下划线分割
			String _model = HumpUtil.underscoreName(model);
			t.binding("ENTITY_MODEL", _model);
			//全部小写，下划线分割			
			t.binding("entity_model", HumpUtil.toLowerCase(_model));	
			
			List<Property> list = tm.getProps();
			for(Property prop : list){
				String str = prop.getJavaType();
				int pos = str.lastIndexOf(".") + 1;
				prop.setJavaType(prop.getJavaType().substring(pos));
			}
			t.binding("fields", list);
			
			StringBuilder toStr = new StringBuilder();
			toStr.append("\""+StrKit.firstCharToUpperCase(model)+ " [");			
			int size = list.size();			
			for(int i=0; i<size; i++){
				Property prop = list.get(i);
				if(i > 0){
					toStr.append("\", ");
				}
				//sysUserId=" + sysUserId + "
				toStr.append(prop.getName()+"=\" "+" +" +prop.getName() + "+ ");
			}
			toStr.append("\"]\"");
			t.binding("toStr", toStr.toString());
			//构造路径#######################			
			String data = t.render();			
			String out = PathUtil.getOutRoot();		
			Map<String,String> map = new HashMap<String,String>();
			//先写死路径吧
			map.put("pkg", pkg2Path(modelPkg));
			map.put("EntityModel", StrKit.firstCharToUpperCase(model));
			String fileName = StringTemplate.getFileName(TemplatePath.ENTITY_JAVA_FILE_OUTPUT,map);		
			FileUtil.write(out+fileName, data);
			
		}catch(Exception e){
			throw new RuntimeException(e);
		}
		
	}
	
	private String pkg2Path(String pkg){
		return pkg.replace(".", "/");
	}

}
